package com.achievo.reportserver.converter;

import java.text.SimpleDateFormat;

import org.apache.commons.beanutils.Converter;


/**
 * 
 * Report Server
 * write at Jan 21, 2010
 * @author <a href="mailto:chinaoryx@gmail.com">Hyman.Zhang (Zhang Haipeng)</a>
 * @version 1.0
 * 
 *
 */
public class DateConverter implements Converter {

	public Object convert(Class type, Object value) {
		if (value == null) {
			return null;
		}
		String p = null;
		if (value instanceof String) {
			p = (String) value;
		}	
		if (value instanceof String[]) {
			String[] tmpArray = (String[]) value;
			if (tmpArray.length > 0) {
				p = tmpArray[0];
			}
		}
		
		if (p == null || p.trim().length() == 0) {
			return null;
		}
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			return df.parse(p.trim());
		} catch (Exception e) {
			return null;
		}
	}

}
